//
//  Indicator.m
//  InstrumentPanel
//
//  Created by nav on 16/6/24.
//  Copyright © 2016年 Chris.C. All rights reserved.
//

#import "Indicator.h"

@implementation Indicator
{
    UIColor *indicator;
    UIColor *ovalColor;
    CAShapeLayer *bigOvallayer;
    CAShapeLayer *smallOvallayer;
    CAShapeLayer *triangleLayer;
    
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        indicator = [UIColor colorWithRed:1.000 green:0 blue:0 alpha:1];
        ovalColor = [UIColor colorWithRed:0.167 green:0.251 blue:0.318 alpha:1];
        bigOvallayer = [CAShapeLayer layer];
        smallOvallayer = [CAShapeLayer layer];
        triangleLayer = [CAShapeLayer layer];
        CGFloat width = CGRectGetWidth(frame);
        CGFloat height = CGRectGetHeight(frame);
        
//        //大圆路径
        CGRect bigOvalRect = CGRectMake(0, height - width, width, width);
        UIBezierPath *OvalPath = [UIBezierPath bezierPathWithOvalInRect:bigOvalRect];
        bigOvallayer.path = OvalPath.CGPath;
        bigOvallayer.strokeColor = ovalColor.CGColor;
        bigOvallayer.fillColor = ovalColor.CGColor;
        [self.layer addSublayer:bigOvallayer];
//
//        //小圆路径
        CGRect smallOvalRect = CGRectInset(bigOvalRect, width/3, width/3);
        UIBezierPath *smallOvalPath = [UIBezierPath bezierPathWithOvalInRect:smallOvalRect];
        smallOvallayer.path = smallOvalPath.CGPath;
        smallOvallayer.strokeColor = indicator.CGColor;
        smallOvallayer.fillColor = indicator.CGColor;
        [self.layer addSublayer:smallOvallayer];
        
        CGPoint topCenterPoint = CGPointMake(width/2, 0);
        float smallovalRadius = width/6;
        float leftBottomPointX = CGRectGetMidX(smallOvalRect) - smallovalRadius *(cos(M_PI_2/90*75));
        float leftBottomPointY = CGRectGetMidY(smallOvalRect) - smallovalRadius *(sin(M_PI_2/90*75));
        CGPoint leftBottomPoint = CGPointMake(leftBottomPointX, leftBottomPointY);
        
        float rightBottomPointX = CGRectGetMidX(smallOvalRect) - smallovalRadius *(cos(M_PI_2/90*105));
        float rightBottomPointY = CGRectGetMidY(smallOvalRect) - smallovalRadius *(sin(M_PI_2/90*105));
        CGPoint rightBottomPoint = CGPointMake(rightBottomPointX, rightBottomPointY);
        
        //三角路径
        UIBezierPath *trianglePath = [UIBezierPath bezierPath];
        [trianglePath moveToPoint:topCenterPoint];
        [trianglePath addLineToPoint:leftBottomPoint];
        [trianglePath addLineToPoint:rightBottomPoint];
        [trianglePath addLineToPoint:topCenterPoint];
        [trianglePath closePath];
        
        triangleLayer.path = trianglePath.CGPath;
        triangleLayer.fillColor = indicator.CGColor;
        triangleLayer.strokeColor = indicator.CGColor;
        [self.layer addSublayer:triangleLayer];
        
        //设置anchorPoint
        NSLog(@"%f  %f",CGRectGetMidY(smallOvalRect),height);
        self.layerAnchorPoint = CGPointMake(0.5, CGRectGetMidY(smallOvalRect)/height);
    }
    return self;
}

@end
